<?php
/**
 * Filename:	GAbstractChart.php
 * Author:		Manish Sinha
 * Date:		22/01/09
 * Details:		This file contains the GAbstractChart class which is
 * 				the base class for all the charts. The common properties
 * 				are defined over here.
 * Project:		GeeCharts
 */

require_once("GChartBase.php");

class GAbstractChart extends GChartBase
{
	//	An array to hold the information used to create the Chart URI
	protected $info = array();
	
	//	An array to hold the data to be represented on the chart
	protected $data = array();
	
	protected $chartColors = array();
	
	protected $chartTitle = array();
	
	protected $titleFont = array();
	
	protected $chartLegend = array();
	
    //	Hold chart legend Position data
	protected $legendPos = array(	"HOR_BOTTOM" => 'b',
									"HOR_TOP" => 't',
									"VERT_BOTTOM" => 'bv',
									"VERT_TOP" => 'tv',
									"VERT_LEFT" => 'l',
									"VERT_RIGHT" => 'r' );
	
	public function __construct($x,$y)
	{
		$this->info['chs']="chs={$x}x{$y}";
	}
	
	public function AddChartColor($colors)
	{
		if(is_array($colors))
		{
			$this->chartColors = array_merge($this->chartColors, $colors);
		}
		
		$this->info['chco'] = "chco=".implode(",",$this->chartColors);
		
		return $this;
	}
	
	public function RemoveColor($color)
	{
		$key = array_search($color,$this->chartColors);
		unset($this->chartColors[$key]);
		
		$this->info['chco'] = "chco=".implode(",",$this->chartColors);
		
		return $this;
	}
	
	public function AddTitle($title,$font=null)
	{
		if(is_array($title))
		{
			foreach($title as $key => $val)
			{
				$title[$key] = str_replace(" ","+",$title[$key]);
			}
			
			$this->chartTitle = $title;
			$this->info['chtt'] = "chtt=".implode("|",$this->chartTitle);
		}
		
		if($font != null)
		{
			$this->titleFont = $font;
			$this->info['chts'] = "chts=".implode(",",$this->titleFont);
		}
		
		return $this;
	}
	
	public function AddChartLegend($legend,$pos=null)
	{
		if(is_array($legend))
		{
			$this->chartLegend = array_merge($this->chartLegend, $legend);
			
			$this->info['chdl'] = "chdl=".implode("|",$this->chartLegend);
			
			if($pos != null)
			{
				$this->info['chdlp'] = "chdlp=".$this->legendPos[$pos];
			}
		}
		return $this;
	}
	
	public function ClearLegend()
	{
		$this->chartLegend = array();
	}
	
	protected function baseURI()
	{
		$uri = array();
		array_push($uri,$this->info['chs']);
		
		if(isset($this->info['chco']))
			array_push($uri,$this->info['chco']);
			
		if(isset($this->info['chtt']))
			array_push($uri,$this->info['chtt']);
			
		if(isset($this->info['chts']))
			array_push($uri,$this->info['chts']);
			
		if(isset($this->info['chdl']))
			array_push($uri,$this->info['chdl']);
			
		if(isset($this->info['chdlp']))
			array_push($uri,$this->info['chdlp']);
			
		return implode("&amp;",$uri);
	}

	public function AddData($tempdata,$index = -1)
	{
		if($index == -1 || isset($this->data[$index]) == false)
		{
			array_push($this->data,$tempdata);
		}
		else
		{
			$this->data[$index] = array_merge($this->data[$index], $tempdata);
		}
		
		if($this->info['cht'] == "lxy")
		{
			if(count($this->data)%2 == 1)
			{
				$this->errormsg = "Odd No of datasets, last one will be discarded";
			} 
		}
		
		$this->info['chd'] = "t:".implode(",", $this->data);
		
		return $this;
	}

	public function DelData($index = -1)
	{
		if ($index == -1)
		{
			array_pop($this->data);
		}
		else if ($index>=0 && $index<count($this->data) )
		{
			unset($this->data[$index]);
		}
		else
		{
			$this->errormsg = "No such array position exists";
		}
		
		return $this;
	}

	public function ShowData()
	{
		echo "<pre>";
		print_r($this->data);
		echo "</pre>";
	}
}
?>
